package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.mapper.ProductAttributeMapper;
import com.wteam.dragon.shop.pojo.bean.ProductAttribute;
import com.wteam.dragon.shop.service.ProductAttributeService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @Author 陈晓辉
 * @Date 2020/6/29 16:38
 */
@Service
@RequiredArgsConstructor
public class ProductAttributeServiceImpl implements ProductAttributeService {
    private final ProductAttributeMapper productAttributeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addProductAttribute(ProductAttribute productAttribute) {
        productAttributeMapper.insert(productAttribute);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delProductAttribute(Set<Long> ids) {
        productAttributeMapper.deleteBatchIds(ids);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editProductAttribute(ProductAttribute productAttribute) {
        productAttributeMapper.updateById(productAttribute);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("编辑成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageProductAttribute(Page<ProductAttribute> page, Integer productTypeId, Integer type) {
        QueryWrapper<ProductAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(type != null, ProductAttribute::getType, type)
                .eq(productTypeId != null, ProductAttribute::getProductTypeId, productTypeId);
        Page<ProductAttribute> productAttributePage = productAttributeMapper.selectPage(page, queryWrapper);
        ResultMessage resultMessage = new ResultMessage();
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("productAttributePage", productAttributePage);
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getProductAttribute(Long id) {
        ValidationUtil.isNull("id不能为空", id);
        ProductAttribute productAttribute = productAttributeMapper.selectById(id);
        ResultMessage resultMessage = new ResultMessage();
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("productAttribute", productAttribute);
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
